import random
import  Map2

class Pos:
    def __init__(self):
        self.x = 1
        self.y = 1
        self.check()

    def check(self):
        map = Map2.Singleton()
        # 遍历二维数组并获取索引
        for i, row in enumerate(map.map_array):  # 遍历每一行并获取行索引
            for j, element in enumerate(row):  # 遍历行中的每一个元素并获取列索引
                # print(f"Element at ({i}, {j}) is: {element}")  # 打印元素及其索引
                if map.map_array[i,j] == 0:
                    self.x = i
                    self.y = j
                    # print(f"{self.x},{self.y}")
                    return



class Enemy:
    def __init__(self, name):
        self.name = name
        # 位置
        self.pos = Pos()
        # 等级
        self.level = 1
        # 主属性
        self.strength = 10
        self.intelligence = 10
        self.agile = 10
        # 副属性
        self.health = 1000
        self.healthMax = 1000
        self.mana = 200
        self.mana = 200
        self.attack = 50
        self.attackSpeed = 50
        self.armor = 50
        # 装备栏
        self.equipmentSlot = []
        # 背包
        self.inventory = []
        self.initLog()

    def initLog(self):
        print(f"{self.name}初始化成功，当前位置[{self.pos.x},{self.pos.y}]")


    def randomMove(self):
        # 定义八个方位的偏移量
        directions = [
            (-1, 0),  # 上
            (1, 0),  # 下
            (0, -1),  # 左
            (0, 1),  # 右
            (-1, -1),  # 左上
            (-1, 1),  # 右上
            (1, -1),  # 左下
            (1, 1)  # 右下
        ]

        # 随机选择一个方向
        dx, dy = random.choice(directions)

        # 位置判断，撞墙和边界等问题处理，没问题才能确认移动成功

        # 计算新的位置
        self.pos.x = self.pos.x + dx
        self.pos.y = self.pos.y + dy
        print(f"{self.name} 位于 [{self.pos.x},{self.pos.y}]")
        return self.pos.x, self.pos.y

    def tack_attack(self, enemy):
        print(f"{self.name} 攻击 {enemy.name} !")
        enemy.take_damage(self.attack)

    def take_damage(self, amount):
        self.health -= amount
        print(f"{self.name} 受到 {amount} 点伤害，当前生命值为 {self.health} !")
        if self.health <= 0:
            self.health = 0
            print(f"{self.name} 被打败了!")

    def heal(self, amount):
        self.health += amount
        print(f"{self.name} 受到 {amount} 点治疗，当前生命值为 {self.health} !")
        if self.health > 100:
            self.health = 100

    def cast_spell(self, spell_cost):
        if self.mana >= spell_cost:
            self.mana -= spell_cost
            print(f"{self.name} casts a spell!")
        else:
            print(f"{self.name} doesn't have enough mana to cast the spell.")

    def add_to_inventory(self, item):
        self.inventory.append(item)

    def get_stats(self):
        return {
            'Name': self.name,
            'Level': self.level,
            'Health': self.health,
            'Mana': self.mana,
            'Strength': self.strength,
            'Intelligence': self.intelligence,
            'Inventory': self.inventory
        }
